package org.yoki.edu.image.utils;

import org.opencv.core.Mat;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/2/9$ 14:08$
 */
public class ImageUtils {

    /**
     * - 反色处理 -
     */
    public Mat inverse(Mat image) {
        int width = image.cols();
        int height = image.rows();
        int dims = image.channels();
        byte[] data = new byte[width * height * dims];
        image.get(0, 0, data);

        int index = 0;
        int r = 0, g = 0, b = 0;
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width * dims; col += dims) {
                index = row * width * dims + col;
                b = data[index] & 0xff;
                g = data[index + 1] & 0xff;
                r = data[index + 2] & 0xff;

                r = 255 - r;
                g = 255 - g;
                b = 255 - b;

                data[index] = (byte) b;
                data[index + 1] = (byte) g;
                data[index + 2] = (byte) r;
            }
        }

        image.put(0, 0, data);
        return image;
    }


}
